﻿namespace Contoso.ActiveDirectory {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    #endregion

    [Serializable]
    public class LocationAttributes : ILocationAttributes {

        #region Members
        public static readonly string[] AttributesToGet;

        #region ILocationAttributes properties
        public string PhysicalDeliveryOfficeName { get; private set; }
        
        /// <summary>
        /// The street address
        /// </summary>
        /// <remarks>Has control characters removed</remarks>
        public string StreetAddress { get; private set; }

        public string City { get; private set; }
        public string StateAbbreviation { get; private set; }
        public string Country { get; private set; }
        public string CountryAbbrevation { get; private set; }
        public int CountryCode { get; private set; }
        public string PostalCode { get; private set; }
        public string RegionName { get; set; }
        #endregion

        #endregion

        #region Constructors
        static LocationAttributes() {

            AttributesToGet = new string[]
            {
                "c",
                "co",
                "countryCode",
                "l",
                "physicalDeliveryOfficeName",
                "postalCode",
                "st",
                "street",
                "streetAddress"
            };

        }

        public LocationAttributes() {
            this.PhysicalDeliveryOfficeName = string.Empty;
            this.StreetAddress = string.Empty;
            this.City = string.Empty;
            this.StateAbbreviation = string.Empty;
            this.CountryAbbrevation = string.Empty;
            this.Country = string.Empty;
            this.PostalCode = string.Empty;
        }
        #endregion

        #region Methods
        public void ProcessResults(Dictionary<string, object[]> attributes) {
            if (attributes.ContainsKey("physicalDeliveryOfficeName")) this.PhysicalDeliveryOfficeName = attributes["physicalDeliveryOfficeName"][0].ToString();
            if (attributes.ContainsKey("streetAddress")) this.StreetAddress = attributes["streetAddress"][0].ToString().RemoveControlCharacters(); ;
            if (string.IsNullOrWhiteSpace(this.StreetAddress)) {
                // OU uses Street attribute
                if (attributes.ContainsKey("street")) this.StreetAddress = attributes["street"][0].ToString().RemoveControlCharacters(); ;
            }            
            if (attributes.ContainsKey("l")) this.City = attributes["l"][0].ToString();
            if (attributes.ContainsKey("st")) this.StateAbbreviation = attributes["st"][0].ToString();
            if (attributes.ContainsKey("postalCode")) this.PostalCode = attributes["postalCode"][0].ToString();
            if (attributes.ContainsKey("c")) this.CountryAbbrevation = attributes["c"][0].ToString();
            if (attributes.ContainsKey("co")) this.Country = attributes["co"][0].ToString();
            if (attributes.ContainsKey("countryCode")) this.CountryCode = Convert.ToInt32(attributes["countryCode"][0]); 
        }

        public override string ToString() {

            var info = new StringBuilder();
            info.AppendFormat("PhysicalDeliveryOfficeName: {0}; ", this.PhysicalDeliveryOfficeName);
            info.AppendFormat("Street: {0}; ", this.StreetAddress);
            info.AppendFormat("City: {0}; ", this.City);
            info.AppendFormat("State: {0}; ", this.StateAbbreviation);
            info.AppendFormat("Country: {0}; ", this.CountryAbbrevation);
            info.AppendFormat("PostalCode: {0}; ", this.PostalCode);

            return info.ToString();

        }
        #endregion
    }
}
